package com.nova.bluetooth;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;

import com.nova.bluetooth.base.BaseActivity;
import com.nova.bluetooth.databinding.ActivityGuideBinding;
import com.nova.bluetooth.helpUtils.SharePreferenceUtils;
import com.nova.bluetooth.helpUtils.ToastUtils;
import com.nova.bluetooth.netWork.NetConstant;
import com.nova.bluetooth.netWork.RetrofitRequest;
import com.nova.bluetooth.netWork.request.UserLogin;
import com.nova.bluetooth.netWork.result.InputAuthCodeResult;
import com.nova.bluetooth.widget.CommonDialog;

public class GuideActivity extends BaseActivity {

    private ActivityGuideBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityGuideBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (policyDialog != null){
            policyDialog.dismissDialog();
        }
    }

    private void initView(){
        showPolicyDialog();
    }

    private CommonDialog policyDialog;
    private void showPolicyDialog(){
        View dialogView = getLayoutInflater().inflate(R.layout.dialog_show_policy, null);
        policyDialog  = new CommonDialog(this,dialogView);

        TextView tvAgree = dialogView.findViewById(R.id.tv_agree);
        tvAgree.setOnClickListener(v -> {
            tvAgree.setEnabled(false);
            tvAgree.setAlpha(0.5f);
            ToastUtils.show("登录中...");
            login();
        });

        TextView tvDisagree = dialogView.findViewById(R.id.tv_un_agree);
        tvDisagree.setOnClickListener(v -> {
            policyDialog.dismissDialog();
            finish();
        });

        policyDialog.setWidth(R.dimen.dp_320);
        policyDialog.setHeight(R.dimen.dp_370);
        policyDialog.setCancelable(false);
        policyDialog.setGravity(Gravity.CENTER);
        policyDialog.showDialog();
    }

    private void login(){
//        String token = (String) SharePreferenceUtils.get(MyApp.getInstance(),
//                NetConstant.IDENTIFY_TOKEN,"");
//        if (token != null && !token.isEmpty()){
//            startActivity(new Intent(GuideActivity.this, AppMainActivity.class));
//            finish();
//            return;
//        }


        String url = NetConstant.URL_BASE + "user/userLoginNew";
        UserLogin userLogin = new UserLogin("86", "13232650111", "090909", "");
        RetrofitRequest.sendPostBodyRequest(url, userLogin, InputAuthCodeResult.class,
                new RetrofitRequest.ResultHandler<InputAuthCodeResult>(MyApp.getInstance()) {
            @Override
            public void onBeforeResult() {
            }

            @Override
            public void onResult(InputAuthCodeResult inputAuthCodeResult) {
                if (inputAuthCodeResult.getCode() == 200){
                    InputAuthCodeResult.DataDTO data = inputAuthCodeResult.getData();
                    SharePreferenceUtils.put(MyApp.getInstance(),NetConstant.IDENTIFY_TOKEN,data.getToken());

                    startActivity(new Intent(GuideActivity.this, AppMainActivity.class));
                    finish();
                }
            }

            @Override
            public void onAfterFailure() {

            }
        });
    }
}